import numpy as np;
import pandas as pd;
from pandas import Series, DataFrame, DatetimeIndex;

'''
numpy，pandas 练习
'''

# numpy test
def numpy_test():
    print("==== 构造 ====")
    l = list(range(1, 10));
    print(l);
    array = np.asarray(l);
    print(array);
    array = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]);
    print(array);
    array = np.arange(1, 10);
    array = np.arange(20).reshape(5, 4);
    print(array);
    print("==== 切片 ====");
    l = list(range(1, 10));
    print(l)
    print(l[1:7]);
    array = np.asarray(l);
    print(array[1:7:2]);
    array = np.arange(20).reshape(5, 4);
    print(array);
    print(array[[1, 3, 2]]);
    print(array[[1,3,4], [1, 1, 0]]);
    print(array[array > 5]);
    print("==== 广播 ====");
    a_1 = np.arange(1, 10);
    a_2 = np.arange(11, 20);
    print(a_1 + a_2);
    a_3 = np.arange(20).reshape(4, 5);
    a_4 = np.arange(1, 6);
    print(a_3);
    print(a_4);
    print(a_3 + a_4);
    print("==== 排序 ====");
    print(a_1);
    print(np.sort(a_1));
    print(-np.sort(-a_1));
    print(a_3);
    print(-np.sort(-a_3, axis=0));
    print("==== 统计 ====");
    print(np.amin(a_3, axis=1));
    print("==== 转list ====");
    print(a_1.tolist());
    print(a_3.tolist());

# series 练习
def series_test():
    print("==== 构造 ====");
    data_dict = {"第一季度":23, "第二季度": 44, "第三季度": 67, "第四季度":25};
    index_list = ["第一季度", "第二季度", "第三季度", "第五季度"];
    s = Series(data=data_dict);
    print(s);
    s = Series(data=data_dict, index=index_list);
    print(s);
    data_list = list(range(1, 10));
    index_list = list("index_%d" % i for i in range(1, 10));
    s = Series(data=data_list);
    print(s);
    s = Series(data=data_list, index=index_list);
    print(s);

    print("==== 新增 ====");
    s["index_10"] = 111;
    print(s);
    print("==== 修改 ====");
    s["insex_9"] = 99;
    print(s);
    print("==== 删除 ====");
    print(s.drop("index_10"));
    print(s);
    print(s.drop(["index_10", "insex_9"]));
    print("==== 查询 ====");
    print(s.index);
    print(s.index.values);
    print(s.values);
    print(s["index_7"], s.get("index_6"));
    print(s[["index_4", "index_6"]]);
    print(s["index_4":"index_6"]);
    print(np.asarray(s));

# DataFrame test
def dataFrame_test():
    print("==== 构造 ====");
    data_dict = {
        "第一季度":[23,43,33,55],
        "第二季度":[33,44,55,3],
        "第三季度":[54,22,5145,45],
        "第四季度":[38,77,66,65],
    }
    f = DataFrame(data=data_dict);
    print(f);
    index_list = ["成都公司", "四川公司", "中国公司", "地球公司", "宇宙公司"];
    column_list = ["第一季度", "第二季度", "第三季度", "第四季度"];
    data_list = np.arange(20).reshape(5, 4);
    f = DataFrame(data=data_list);
    print(f);
    f = DataFrame(data=data_list, index=index_list, columns=column_list);
    print(f);
    print("==== 新增 ====");
    f["是否合格"] = (f["第四季度"] >=11 );
    print(f);
    print("==== 删除 ====");
    print(f.drop("是否合格", axis=1));
    print(f.drop(["第四季度", "是否合格"], axis=1));
    print(f.drop("宇宙公司", axis=0));
    print("==== 查询 ====");
    print(f.shape);
    print(f["第三季度"]);
    print(f[["第一季度", "第三季度"]]);
    print(f.loc["成都公司"]);
    print(f.loc[["成都公司", "宇宙公司"]]);
    print("----------------------------")
    for index, row in f.iterrows():
        print(index, row);
    print("----------------------------")
    for row in f.itertuples():
        print(row.Index, row.第二季度);
    print("----------------------------")
    for x, y in zip(f["第二季度"], f["第三季度"]):
        print(x, y);

# 时间序列
def datetimeIndex_test():
    dti = pd.date_range(start="20230501", end="20230523");
    print(dti);
    dti = pd.date_range(start="20230501", end="20230523", freq="12H");
    print(dti);

# 进阶功能
def fun_01():
    data_list = list(range(1, 10));
    index_list = list("index_%d" % i for i in range(1, 10));
    s = Series(data=data_list);
    index_list = ["成都公司", "四川公司", "中国公司", "地球公司", "宇宙公司"];
    column_list = ["第一季度", "第二季度", "第三季度", "第四季度"];
    data_list = np.arange(20).reshape(5, 4);
    f = DataFrame(data=data_list, index=index_list, columns=column_list);
    print(s);
    print(f);
    print("======== 排序 =========");
    print(s.sort_index(ascending=False));
    print(s.sort_values(ascending=False));
    print(f.sort_index(axis=0, ascending=False));
    print(f.sort_values(by="第三季度", ascending=False));

    print("======== 拆分合并 =========")
    print(f["第一季度"]);
    print(f["第二季度"]);
    print(pd.concat([f["第一季度"], f["第二季度"]], axis=1));
    print("-------------------")
    data_list = ["1,34,32", "32,43,54", "43,54,23", "43,11,44", "43,22,55"];
    index_list = ["成都公司", "四川公司", "中国公司", "地球公司", "宇宙公司"];
    s_temp = Series(data=data_list, index=index_list);
    print(s_temp);
    f["第五季度"] = s_temp;
    print(f);
    print(f["第五季度"].str.split(",", expand=True).rename(columns={0:"额外1",1:"额外2",2:"额外3"}));
    print(pd.concat([
        f["第一季度"],
        f["第三季度"],
        f["第五季度"].str.split(",", expand=True).rename(columns={0: "额外1", 1: "额外2", 2: "额外3"})
    ], axis=1));

    print("======== apply =========")
    f["第四季度"] = f["第四季度"].apply(lambda item:item + 10);
    print(f);




if __name__ == '__main__':
    fun_01();
